package com.store.app.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.R;
import com.store.app.bean.BookedBean;
import com.store.app.bean.ShopCarBean;
import com.store.app.bean.WypGoodsBean;

import java.util.List;

import de.greenrobot.event.EventBus;

public class ShoppingCarItemListViewAdapter extends BaseAdapter {
    private List<WypGoodsBean> bookedBeans;
    private Context context;
    private int parentPosition;//所属父窗体哪个下标

    public ShoppingCarItemListViewAdapter(List<WypGoodsBean> bookedBeans, Context context, int parentPosition) {
        this.bookedBeans = bookedBeans;
        this.context = context;
        this.parentPosition = parentPosition;
    }

    @Override
    public int getCount() {
        return bookedBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return bookedBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final WypGoodsBean bookedBean = bookedBeans.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_shop_card, null);
            holder.btnAdd = (TextView) convertView.findViewById(R.id.btnAdd);
            holder.btnJian = (TextView) convertView.findViewById(R.id.btnJian);
            holder.btnContent = (TextView) convertView.findViewById(R.id.btnContent);
            holder.img_head = (ImageView) convertView.findViewById(R.id.img_head);
            holder.txt_name = (TextView) convertView.findViewById(R.id.txt_name);
            holder.txt_price = (TextView) convertView.findViewById(R.id.txt_price);
            holder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            holder.c_check = (CheckBox) convertView.findViewById(R.id.c_check);
            holder.c_check_lin = (TextView)convertView.findViewById(R.id.c_check_lin);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.btnContent.setText(bookedBean.getBuyCount() + "");
        holder.txt_name.setText(bookedBean.getTitle() + "");
        holder.txt_price.setText("¥ " + bookedBean.getLastMoney() + "");
        holder.tv_type.setText(bookedBean.getSpecificationsBeanList().get(bookedBean.getChooseIndexSpeci()).getDesc1()+ "");
        holder.c_check.setChecked(bookedBean.ischecek());
        ImageLoader.getInstance().displayImage(bookedBean.getPic_show_url(), holder.img_head, options);
//        holder.c_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                EventBus.getDefault().post(new ShopCarBean(2, isChecked, position, parentPosition));
//            }
//        });
        holder.c_check_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    EventBus.getDefault().post(new ShopCarBean(2, ! bookedBean.ischecek(), position, parentPosition));
            }
        });
        holder.btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new ShopCarBean(3, false, position, parentPosition));
            }
        });
        holder.btnJian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new ShopCarBean(4, false, position, parentPosition));
            }
        });
        return convertView;
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.default_picture_s)
            .showImageOnFail(R.drawable.default_picture_s)
            .showStubImage(R.drawable.default_picture_s)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    class ViewHolder {
        TextView btnJian;           //减
        TextView btnContent;        //数量
        TextView btnAdd;            //加
        ImageView img_head;         //图片
        TextView txt_name;          //商品名称
        TextView txt_price;         //商品价格
        TextView tv_type;         //商品规格
        CheckBox c_check;//         //是否选中
        TextView c_check_lin;
    }
}
